package com.system.controller;

import com.system.annotation.SystemLogAnnotation;
import com.system.common.BaseController;
import com.system.common.ResponseCode;
import com.system.config.jwt.JwtConfig;
import com.system.constants.SysLogType;
import com.system.entity.dto.LoginDTO;
import com.system.entity.manage.UserDataVO;
import com.system.entity.vo.RspResultVO;
import com.system.entity.vo.UserInfoVO;
import com.system.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 登陆相关接口
 *
 * @author 佛冷
 * @date 2019/10/8
 */
@Api(tags = "登陆相关接口")
@RestController
@RequestMapping("/")
public class LoginController extends BaseController {

    @Autowired
    private UserService userService;

    @Autowired
    private JwtConfig jwtConfig;

    @ApiOperation("用户登录接口")
    @PostMapping("/login")
    @SystemLogAnnotation(operationName = "用户登录", businessType = SysLogType.LOGIN)
    public RspResultVO login(@RequestBody LoginDTO dto) {

        String username = dto.getUsername();
        String password = dto.getPassword();
        // 验证用户信息
        int code = userService.verifyUserInfo(username, password);
        if (code == 1) {
            return returnResult(ResponseCode.LOGIN_ERROR.getCode(), "用户名不存在");
        }
        if (code == 2) {
            return returnResult(ResponseCode.LOGIN_ERROR.getCode(), "该用户被禁用");
        }
        if (code == 3) {
            return returnResult(ResponseCode.LOGIN_ERROR.getCode(), "密码错误");
        }
        UserDataVO userDataVO = userService.getUser(username);
        String token = jwtConfig.createToken(userDataVO.getUserName());
        userDataVO.setToken(token);
        return returnSuccess(userDataVO);

    }


    /**
     * 根据请求头的token获取userId
     *
     * @param request
     * @return
     */
    @GetMapping("/user/info")
    @SystemLogAnnotation(operationName = "获取用户信息", businessType = SysLogType.SELECT_ONE)
    public RspResultVO<UserInfoVO> getUserInfo(HttpServletRequest request) {
        String usernameFromToken = jwtConfig.getUsernameFromToken(request.getHeader("token"));
        UserInfoVO userInfoVO = userService.getUserInfo(usernameFromToken);
        return returnSuccess(userInfoVO);
    }

    /**
     * 根据请求头的token获取userId
     *
     * @param request
     * @return
     */
    @PostMapping("/logout")
    @SystemLogAnnotation(operationName = "获取用户信息", businessType = SysLogType.LOGIN_OUT)
    public RspResultVO logout(HttpServletRequest request) {
        return returnSuccess();
    }


    public static void main(String[] args) {
        System.out.println(Base64Utils.decodeFromString("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJzdWIiOiLmtYvor5XnlKjmiLciLCJpYXQiOjE2MTgzODk5MjgsImV4cCI6MTYxODM5MzUyOH0.0duCHbKoA8XF7P2SJBrYNo7n3Wvu3EZQsmTwEEXncB_appmtu2NmBxh2GtahH0mH7cBPXWZK2TX3N3qa0PRLEA"));
    }

}
